package compile

import (
	"fmt"
	"path/filepath"
	"sail/general"
)

const _CHRES_WARNING = 0
const _CHRES_ERROR = 1

// almost every checking function will return []*checkResult
// if everything is ok, []*checkResult will be empty. (length == 0)
type checkResult struct {
	resType int
	reason  string
}

// Check this class (.sail) file and package declare is valid or not.
func checkClassNameAndPackage(fileNode *ClassFile,
	filePath string, baseDir string) []*checkResult {

	var results []*checkResult

	publicClsName := fileNode.PublicClassName
	dir, realFileName := filepath.Split(filePath)

	pkgPath := dotPathToFilePath(fileNode.Package.PackagePath)
	pkgPath, err := filepath.Abs(pkgPath) // to ABS path

	if err != nil {
		plainError(err.Error(), true)
	}

	if pkgPath != dir {
		results = append(results, &checkResult{
			reason: "invalid package declaration",
		})
	}

	if publicClsName != "" &&
		publicClsName+general.SailSourceExt != realFileName {

		results = append(results, &checkResult{
			reason: fmt.Sprintf("Class %s should not be public since the class name "+
				"does not match the file name", publicClsName),
		})
	}

	return results
}
